﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Cosmetic.DataAccess;
using Cosmetic.DataEntity;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Cosmetic.Logic
{
    public class numbersManger : LogicBase
    {
        private numbers m_numbers = new numbers();
        /// <summary>
        /// 新增一个卡号
        /// </summary>
        /// <param name="anumber">The product want delete</param>

        public void Insert(numbers  anumber)
        {
            m_CosmeticDC.numbers .InsertOnSubmit(anumber );
            m_CosmeticDC.SubmitChanges();
        }

        /// <summary>
        /// 删除一个卡号
        /// </summary>
        /// <param name="aProduct">The product want delete</param>
        public void Delete(numbers anumber)
        {
            m_CosmeticDC.numbers.DeleteOnSubmit(anumber );
            m_CosmeticDC.SubmitChanges();
        }
        /// <summary>
        /// 查询所有的卡号
        /// </summary>
        /// <param name="anumber">The product want delete</param>

        public List<numbers > GetAll()
        {
            return (from theActivity in m_CosmeticDC.numbers 
                    select theActivity).ToList();
        }

        /// <summary>
        /// 查询所有的卡号
        /// </summary>
        /// <param name="anumber">The product want delete</param>
      

        public IEnumerable GetAllnumbers()
        {
            var result = from theFocus in m_CosmeticDC.numbers
                         join theProduct in m_CosmeticDC.Users
                         on theFocus.userid equals theProduct.UserId

                         select new
                         {
                             id = theFocus.id,
                             number = theFocus.number,
                             blances = theFocus.blances,
                             isuserd = theFocus.IsUsed,

                             ynumber = theFocus.ynumber,

                             blance = theFocus.blance,
                             tel = theProduct.tel

                         };
            return result;
        }

        /// <summary>
        /// 判断是否存在次卡号
        /// </summary>
        /// <param name="anumber">The product want delete</param>

        public bool CheckExists(string uername)
        {
            List<numbers> theProductList = (from theProduct in m_CosmeticDC.numbers
                                            where theProduct.number == uername
                                            select theProduct).ToList();
            return theProductList.Count > 0;
        }
        /// <summary>
        /// 判断是否存在次卡号
        /// </summary>
        /// <param name="anumber">The product want delete</param>

        public numbers CheckExistes(string uername)
        {
            List<numbers> theProductList = (from theProduct in m_CosmeticDC.numbers
                                            where theProduct.number == uername
                                            select theProduct).ToList();
            if (theProductList.Count == 1)
            {
                return theProductList[0];
            }
            return null;
        }
        /// <summary>
        /// 根据ID获得所有的结果
        /// </summary>
        /// <param name="anumber">The product want delete</param>

        public numbers  GetProductById(int  aProductId)
        {
            return m_CosmeticDC.numbers .Single(theProect => theProect.id  == aProductId);
        }
        /// <summary>
        /// 根据用户ID获得所有的结果
        /// </summary>
        /// <param name="anumber">The product want delete</param>

        public numbers GetnumbersByuserid(string  userid)
        {
            List<numbers> theUserList = (from theUser in m_CosmeticDC.numbers
                                         where theUser.number  == userid
                                         select theUser).ToList();
            if (theUserList.Count == 1)
            {
                return theUserList[0];
            }
            return null;
        }


        public numbers GetnumbersByuserid1(Guid  userid)
        {
            List<numbers> theUserList = (from theUser in m_CosmeticDC.numbers
                                         where theUser.userid  == userid
                                         select theUser).ToList();
            if (theUserList.Count >= 1)
            {
                return theUserList[0];
            }
            return null;
        }
        public static void UpdateInformation(int id, bool a, Guid bb, decimal?  cc)
        {
            string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                con.Open();
                string cmdStr = string.Empty;

                cmdStr = "UPDATE [numbers]  SET [IsUsed] ='" + a + "'  ,[userid] = '" + bb + "',[blance] = '" + cc + "' WHERE id=" + id + "";
                SqlCommand com = new SqlCommand(cmdStr, con);
                com.ExecuteNonQuery();
            }
            finally
            {
                con.Close();
            }
        }

        public static void UpdateInformations(int id,Guid bb)
        {
            string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                con.Open();
                string cmdStr = string.Empty;

                cmdStr = "UPDATE [numbers]  SET [ProductId] = '" + bb + "' WHERE id=" + id + "";
                SqlCommand com = new SqlCommand(cmdStr, con);
                com.ExecuteNonQuery();
            }
            finally
            {
                con.Close();
            }
        }


    }

}
